package org.maxgroundstation.groundstation.comm.service.wifi;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import javax.swing.SwingUtilities;

import org.maxgroundstation.groundstation.comm.service.Service;
import org.maxgroundstation.groundstation.comm.service.ServiceCommunication;
import org.maxgroundstation.groundstation.comm.service.ServiceDataFrame;
import org.maxgroundstation.groundstation.comm.service.ServiceUtil;

/**
 * The WiFi signal strength service.
 * @author Scott Davis
 * 
 * Copyright (C) 2010  Scott Davis
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class WiFiService extends Service {

    // Static members
    public static final String SERVICE_ID = ServiceUtil.getServiceID("wifi_ss");
    public static final String TYPE = "wfi";
    
    // Service event type.
    public static final String WIFI_UPDATE = "wifi update";
    
    // Data members
    private float wifiSignalStrength;
    
    /**
     * Constructor
     * @param comm the service communication.
     */
    public WiFiService(ServiceCommunication comm) {
        // Use Service constructor
        super(comm);
        
        // Initialize data members.
        wifiSignalStrength = 0F;
    }
    
    /**
     * Gets the rover's WiFi signal strength.
     * @return signal strength percentage.
     */
    public float getWiFiSignalStrength() {
        return wifiSignalStrength;
    }
    
    @Override
    public String getID() {
        return SERVICE_ID;
    }

    @Override
    public void handleResponse(ServiceDataFrame dataFrame) {
        
        // Get new WiFi signal strength value.
        ByteBuffer buff = ByteBuffer.wrap(dataFrame.getDataBytes());
        buff.order(ByteOrder.LITTLE_ENDIAN);
        float newWifiSignalStrength = buff.getFloat();
        
        //System.out.println("Receiving WiFi Signal Strength: " + newWifiSignalStrength);
        
        // Fire wifi update service event if wifi signal strength has changed.
        if (this.wifiSignalStrength != newWifiSignalStrength) {
            this.wifiSignalStrength = newWifiSignalStrength;
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    fireServiceEvent(WIFI_UPDATE, new Float(wifiSignalStrength));
                }
            });
        }
    }

    @Override
    public void initialize() {
        // Do nothing.
    }

    @Override
    public boolean isDefaultService() {
        return false;
    }

    @Override
    public boolean isSubscribable() {
        return true;
    }

    @Override
    public String getType() {
        return TYPE;
    }
}